#ifndef EYAS_THREAD_DEFAULT_THREADPOOL_H_
#define EYAS_THREAD_DEFAULT_THREADPOOL_H_

#include <queue>
#include <map>
#include <boost/thread/thread.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>

#include "../conf/def.h"

#include "basic/singleton.hpp"
#include "thread_pool_worker.h"
#include "gc/gc.h"
#include "../task/thread_task.hpp"


#ifdef ENABLE_THREAD_POOL_IOSERVICE
#include <boost/asio/io_service.hpp>
#endif

namespace eyas {
namespace thread {

class EYAS_THREAD_API thread_pool {
public:
	thread_pool();
	thread_pool(unsigned int capacity);
	thread_pool(boost::asio::io_service& service);
	thread_pool(boost::asio::io_service& service, unsigned int capacity);
	virtual ~thread_pool();
private:
	typedef std::queue<thread_pool_worker_ptr> thread_queue;
	// key:线程Id
	typedef std::map<boost::thread::id, thread_pool_worker_ptr> thread_map;

	unsigned int capacity_;
	// 工作线程集合
	thread_map thread_map_;

	// io_service是否由本类实例化
	bool ioservice_myself_;
	boost::asio::io_service* service_;
	boost::asio::io_service::work *work_;
private:
	void capacity(unsigned int capacity);

	friend class thread_pool_worker;
	void work(thread_pool_worker* worker);
	//void do_kill(unsigned int th_id);
public:
	unsigned int capacity(){return this->capacity_;}
	// 异步执行任务
	template<typename F, typename... Args>
	void schedule(F threadfunc, Args... args);
};
typedef boost::shared_ptr<thread_pool> thread_pool_ptr;

template<typename F, typename... Args>
void thread_pool::schedule(F threadfunc, Args... args){
	this->service_->post(boost::bind(threadfunc, args...));
}

}
}
#endif
